/*******************************************************


           Individual Productivity Analysis
              

*********************************************************/



set more off 
clear all
set segmentsize 3g


* Load Data
use $Data/Final/individual_produc_clean.dta, clear


* Comparing productivity of Hindus and Muslims

local indiv_prod 1

if `indiv_prod' == 1{
	g smallbags_hourly = small_bags/4
	local outcome = "smallbags_hourly" 
	
	local outtitle = "Hourly Productivity (Number of bags made)"
	local rangemin = 0
	local rangejump = 3
	local rangemax = 9
	
	g beta = .
	g u95 = .
	g l95 = .
	g order = _n
	
	// Initialise beta as the control mean
	qui su `outcome' if b1_hindu==0
	local control_mean = r(mean)
	replace beta = r(mean) in 1
	
	// Hindu and Muslim
	reghdfe `outcome' b1_hindu, absorb(i.date i.leadin_slot)  vce(robust)
	
	replace beta = `control_mean' + _b[b1_hindu] in 4
	replace u95 = beta + _se[b1_hindu]*invttail(e(df_r),0.025) in 4
	replace l95 = beta - _se[b1_hindu]*invttail(e(df_r),0.025) in 4	
	
	** Get p-value
	test b1_hindu
	if r(p)<0.001 {
		local phindu = "{it:p}<0.001"
	}
	else if r(p)<0.01 {
		local phindu = "{it:p}<0.01"
	}
	else if r(p)>=0.995 {
		local phindu = "{it:p}=1"
	}
	else {
		local phindu = "{it:p}=0"+string(round(r(p),.01))
	}
	
	// get p-value bracket positions
	qui su u95 if order<=3
	local pbrac1Y = r(max) + 3
	local pbrac1Yl = r(max) + 1.5
	local pbrac1Yu = r(max) + 3.1
	
	local pbrac2Y = `pbrac1Y' + 5
	local pbrac2Yl = `pbrac1Yl' + 5
	local pbrac2Yu = `pbrac1Yu' + 5
	
	local pbrac3Y = `pbrac1Y' + 10
	local pbrac3Yl = `pbrac1Yl' + 10
	local pbrac3Yu = `pbrac1Yu' + 10	
	
	qui su u95 if inlist(order,1,4)
	local pbrac4Y = r(max) + 3
	local pbrac4Yl = r(max) + 1.5
	local pbrac4Yu = r(max) + 3.1
	
	// get bar mean y-position
	qui su beta
	local meanY = beta*0.5
	
	// Get means
	g betashort = string(round(beta,.1))
	forv i=1/4 {
		local mean`i' = betashort in `i'
	}	
	
	// Now Graph
	drop if order==2 | order==3
	replace order=2 if order==4
	twoway (bar beta order if order==1, barw(0.6) fc(white) lc(black) lw(medium)) ///
			(bar beta order if order==2, barw(0.6) fc(turquoise*0.3) lc(black) lw(medium)) ///	
		   (rcap u95 l95 order if order<=2, lc(black)) ///
		   (pci `pbrac4Y' 1 `pbrac4Y' 1.3 `pbrac4Yl' 1 `pbrac4Yu' 1 `pbrac4Y' 2 `pbrac4Y' 1.7 `pbrac4Yl' 2 `pbrac4Yu' 2, lc(black)), ///
		   ylabel(0(`rangejump')`rangemax', labsize(3.5) nogrid) ///
		   xlabel(, nogrid) ///
		   ysc(r(0 `rangemax')) ///
		   xlabel(1 "Muslim" 2 "Hindu", noticks nogrid labsize(3.5)) ///
		   xtick(0.5(1)2.5) ///
		   ytitle("`outtitle'", size(3.5)) ///
		   xsize(4) ///
		   xtitle("") ///	
		   text(`meanY' 1 "`mean1'", size(3)) ///
		   text(`meanY' 2 "`mean4'", size(3)) ///
		   text(`pbrac4Y' 1.5 "`phindu'", size(2.5)) ///				   
		   legend(off)
		   graph export "$Output/Figures/individual_produc.pdf", replace 

	
	
}
